package newcoder_exp.justPractise;

import java.util.Arrays;

import org.junit.Test;

public class Pra3_backpack {
    //0-1背包问题

    public int[] backpack(int[] v, int[] m, int M) {
        int n = v.length;
        int[][] f = new int[n + 1][M + 1];
        for (int j = 0; j <= M; j++) {
            f[n][j] = 0; //没东西可以拿
        }
        for (int i = n - 1; i >= 0; i--) {
            for (int j = 0; j <= M; j++) {
                f[i][j] = f[i + 1][j];
                if (j >= m[i]) {
                    f[i][j] = Math.max(f[i][j], f[i + 1][j - m[i]] + v[i]);
                }
            }
        }
        System.out.println(Arrays.deepToString(f));
        res = new int[n]; //0-不拿，1-拿
        consider(f, 0, M, m);
        return res;
    }
    int[] res = null;
    private void consider(int[][] f, int idx, int m, int[] p) {
        for (int i = 0; i < f.length - 1; i++) {
            if (f[i][m] == f[i + 1][m]) res[i] = 0;
            else {
                res[i] = 1;
                m -= p[i];
            }
        }
    }

    @Test
    public void test() {
        int[] v = new int[]{6,3,5,4,6};
        int[] m = new int[]{2,2,6,5,4};
        System.out.println(Arrays.toString(backpack(v, m, 10)));
    }
}
